package com.five.monkey.feign.provider.config;

import org.apache.curator.RetryPolicy;
import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.CuratorFrameworkFactory;
import org.apache.curator.retry.ExponentialBackoffRetry;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author 狻猊
 * @date 2021/10/9 2:17 下午
 */
@Configuration
public class ZkConfig {

    @Value("${spring.curator.retryCount}")
    private int retryCount;
    @Value("${spring.curator.elapsedTimeMs}")
    private int elapsedTimeMs;
    @Value("${spring.curator.connectZK}")
    private String connectZK;
    @Value("${spring.curator.sessionTimeoutMs}")
    private int sessionTimeoutMs;
    @Value("${spring.curator.connectionTimeoutMs}")
    private int connectionTimeoutMs;

    @Bean
    public CuratorFramework zkClient() {
        RetryPolicy retryPolicy = new ExponentialBackoffRetry(elapsedTimeMs, retryCount);
        CuratorFramework zkClient = CuratorFrameworkFactory.newClient(connectZK, sessionTimeoutMs, connectionTimeoutMs, retryPolicy);
        zkClient.start();
        return zkClient;
    }
}
